package com.o.Reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 通用编程调用任意一个方法
 *
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-30-15:45 15:45
 */
public class TestInvokeAnything {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        invokeAny(new B(),"b",new Class[]{int.class,double.class},12,12.5);
    }
    //invokeAny(对象,方法名,方法实参类型,传入(多个)参数)
    public static void invokeAny(Object object,String methodName,Class[] types,Object... args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> aClass = object.getClass();
        Method method = aClass.getMethod(methodName,types);
        method.invoke(object,args);
    }
}
class B{
    public void b(int a,double b){}
}
